namespace("mk");

mk.controlManager = {
    leftMouseButtonDown : false
    , rightMouseButtonDown : false
    , character: null
    , angles :{}
    ,init: function(character) {
        var context = this;
        this.character = character
        window.onmousedown = function(e) {
            if(e.button==0) {
                context.leftMouseButtonDown = true;
            }
            else if(e.button==2) {
                context.rightMouseButtonDown = true;
            }
            e.stopPropagation();
            return false;
        };
        window.onmouseup = function(e) {
            if(e.button==0) {
                context.leftMouseButtonDown = false;
            }
            else if(e.button==2) {
                context.rightMouseButtonDown = false;
            }
            e.stopPropagation();
            return false;
        };
        document.getElementById("battle-field").onmousemove = function(e) {
            if(context.leftMouseButtonDown || context.rightMouseButtonDown)
            mk.SocketManager.sendMessage({type:"hand", mx:e.offsetX, my:e.offsetY, lmb:context.leftMouseButtonDown, rmb:context.rightMouseButtonDown});
        };

        $(document).bind('keydown', 'right', function() {
            if(context.character.__action!="go"){
                mk.SocketManager.sendMessage({type:"action", action:"go", moveKX:5, x:context.character.x, y:context.character.y});
            }
            context.character.moveKX = 5;
            return false;
        });
        $(document).bind('keydown', 'left', function() {
            if(context.character.__action!="back"){
                mk.SocketManager.sendMessage({type:"action", action:"back", moveKX:-4, x:context.character.x, y:context.character.y});
            }
            return false;
        });
        $(document).bind('keydown', 'Ctrl+right', function() {
            mk.SocketManager.sendMessage({type:"direction", direction:1});
            return false;
        });
        $(document).bind('keydown', 'Ctrl+left', function() {
            mk.SocketManager.sendMessage({type:"direction", direction:-1});
            return false;
        });
        $(document).bind('keydown', 'up', function() {
            if(context.character.__action!="go"){
                mk.SocketManager.sendMessage({type:"action", action:"go", moveKY:-5, x:context.character.x, y:context.character.y});
            }
            return false;
        });
        $(document).bind('keydown', 'down', function() {
            if(context.character.__action!="go"){
                mk.SocketManager.sendMessage({type:"action", action:"go", moveKY:5, x:context.character.x, y:context.character.y});
            }
            context.character.moveKY = 5;
            return false;
        });
        $(document).bind('keyup', 'right', function() {
            //if(context.character.moveKY == 0)
                mk.SocketManager.sendMessage({type:"action", action:"stay", moveKX:0, x:context.character.x, y:context.character.y});
            return false;
        });
        $(document).bind('keyup', 'left', function() {
            //if(context.character.moveKY == 0)
                mk.SocketManager.sendMessage({type:"action", action:"stay", moveKX:0, x:context.character.x, y:context.character.y});

            return false;
        });
        $(document).bind('keyup', 'up', function() {
            //if(context.character.moveKX == 0)
                mk.SocketManager.sendMessage({type:"action", action:"stay", moveKY: 0, x:context.character.x, y:context.character.y});
            return false;
        });
        $(document).bind('keyup', 'down', function() {
            //if(context.character.moveKX == 0)
                mk.SocketManager.sendMessage({type:"action", action:"stay", moveKY: 0, x:context.character.x, y:context.character.y});
            return false;
        });
    }
    , calculateAngles: function(character, x, y, lmd, rmd) {
        var dx = x-character.x || 1;
        var dy = y-character.y || 1;
        var dz = Math.sqrt(dx*dx+dy*dy);
        var angles = {};
        if(lmd) {
            angles.p1 = (Math.asin(dy/dz)*180)/Math.PI;
            if((dx<0 && character.direction>0) || (dx>0 && character.direction<0))
                angles.p1 = -angles.p1-180;
            angles.l1= (dz*0.4- 135);
            angles.l1 = angles.l1 > 0 ? 0 : angles.l1
            angles.k1 = angles.l1 = angles.l1 < -120 ? -120 : angles.l1;
        }
        else if(rmd) {
            angles.p2 = (Math.asin(dy/dz)*180)/Math.PI;
            if((dx<0 && character.direction>0) || (dx>0 && character.direction<0))
                angles.p2 = -angles.p2-180;
            angles.l2= (dz*0.4- 135);
            angles.l2 = angles.l2 > 0 ? 0 : angles.l2
            angles.k2 = angles.l2 = angles.l2 < -120 ? -120 : angles.l2;
        }
        else {
            return null
        }
        return angles;
    }
};